package com.sqllite.springbootsqlitejdbc.frame;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReflectionMapper {
    public static <T> T mapRow(ResultSet rs, Class<T> clazz) throws SQLException {
        try {
            T instance = clazz.getDeclaredConstructor().newInstance();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                field.set(instance, rs.getObject(field.getName()));
            }
            return instance;
        } catch (Exception e) {
            throw new SQLException("Error mapping row to class " + clazz.getName(), e);
        }
    }
}
